//******************************************************************************
//this code is protected by the GNU affero GPLv3
//author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com>
//                        <digital.ragnarok AT gmail dot com>
//******************************************************************************
//------------------------------------------------------------------------------
//compiler stuff
//------------------------------------------------------------------------------
#include <stdarg.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//ulinux stuff
//------------------------------------------------------------------------------
#include <ulinux/compiler_types.h>
#include <ulinux/types.h>
#include <ulinux/sysc.h>

#include <ulinux/utils/ascii/ascii.h>
#include <ulinux/utils/ascii/string/string.h>
#include <ulinux/utils/ascii/string/vsprintf.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//protocol
//------------------------------------------------------------------------------
#include <wayland/display.h>
#include <wayland/callback.h>
#include <wayland/compositor.h>
#include <wayland/registry.h>
#include <wayland/shm.h>
#include <wayland/shm_pool.h>
#include <wayland/buffer.h>
#include <wayland/seat.h>
#include <wayland/output.h>
#include <wayland/keyboard.h>
#include <wayland/pointer.h>
#include <wayland/wayland.h>
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//our private stuff
//------------------------------------------------------------------------------
#include "globals.h"
#include "objs.h"
#include "srv/display.h"
#include "srv/shm.h"
#include "fake_shm_pool.h"
#include "wire/wl_callback.h"
#define CALLBACK_C
#include "callbacks.h"
#undef CALLBACK_C
//------------------------------------------------------------------------------

void callback_finish(k_u32 serial)
{
  PERRC("FINISHED\n");
  (void)serial;
}

void callback_shm_pool_destroy_buffers(k_u32 serial)
{
  PERRC("destroying the fake shm pool buffers...\n");
  (void)serial;

  fake_shm_pool_buffers_destroy();

  PERRC("syncing for finish\n");
  display_sync(callback_finish);
}

void callback_shm_destroy_pool(k_u32 serial)
{
  PERRC("destroying the fake shm pool...\n");
  (void)serial;

  fake_shm_pool_destroy();

  PERRC("syncing for fake shm pool buffers destruction\n");
  display_sync(callback_shm_pool_destroy_buffers);
}

void callback_shm_pool_create_buffers(k_u32 serial)
{
  PERRC("creating some fake shm pool buffers...\n");
  (void)serial;

  fake_shm_pool_buffers_create();

  PERRC("syncing for shm pool destruction (wire invisibility)\n");
  display_sync(callback_shm_destroy_pool);
}

void callback_shm_create_pool(k_u32 serial)
{
  PERRC("creating the fake shm pool received...\n");
  (void)serial;

  fake_shm_pool_create();

  PERRC("syncing for destroying the fake shm pool\n");
  display_sync(callback_shm_pool_create_buffers);
}

void callback_shm_binding(k_u32 serial)
{
  PERRC("binding to the shm object received...\n");
  (void)serial;

  shm_bind();

  PERRC("syncing for creating a shm pool after shm format events\n");
  display_sync(callback_shm_create_pool);
}
